//
//  HomeOthersListenCell.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2018/12/21.
//  Copyright © 2018 Gox_WallyDiao. All rights reserved.
//

import UIKit

protocol HomeOthersListenCellDelegate:NSObjectProtocol {
    func HomeOthersListenCellClick(id:Int)
    func HomeOthersListenHeaderCellClick(id:Int, name:String)
}

class HomeOthersListenCell: UITableViewCell, OthersListenReusableViewDelegate {
    
    
    weak var delegate:HomeOthersListenCellDelegate?
    
    fileprivate let OthersListenCellID = "OthersListenCell"
    fileprivate let OthersListenReusableViewID = "OthersListenReusableView"
    
//    fileprivate var viewCT:UIViewController!
    
    var model = OtherListenModel()
    var modelList: OtherListenModel? {
        didSet {
            guard let model = modelList else { return }
            self.model = model
            self.collectionView.reloadData()
        }
    }
    
    fileprivate lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout.init()
        layout.itemSize = CGSize.init(width: (SCREEN_WIDTH - 36) / 3, height: 146)
        layout.headerReferenceSize = CGSize.init(width: SCREEN_WIDTH, height: 55)
        layout.sectionInset = UIEdgeInsets.init(top: 0, left: 9, bottom: 0, right: 9)
        layout.minimumInteritemSpacing = 9
        let collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.register(OthersListenCell.self, forCellWithReuseIdentifier: OthersListenCellID)
        collectionView.register(OthersListenReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: OthersListenReusableViewID)
        collectionView.delegate = self
        collectionView.dataSource = self
        return collectionView
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setUI()
    }

    
    func setUI() {
        self.collectionView.frame = FRAME_CGFloat(0, 0, SCREEN_WIDTH, 217)
        self.collectionView.backgroundColor = WhiteBackColor
        self.addSubview(self.collectionView)
        
        let lineView = UIView.init(frame: FRAME_CGFloat(0, 216, SCREEN_WIDTH, 1))
        lineView.backgroundColor = RGB_Same(240)
        collectionView.addSubview(lineView)
        setCollectionView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

//  MARK: - ###### 私有方法 ######
extension HomeOthersListenCell {
    func setCollectionView() {
    
    }
}


//  MARK: - ###### 公有方法 ######
extension HomeOthersListenCell {
    
}

//  MARK: - ###### 协议方法 ######
extension HomeOthersListenCell:UICollectionViewDelegate,UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 3
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: OthersListenCellID, for: indexPath) as! OthersListenCell
        let model = self.model.subProgram
        cell.imageView.sd_setImage(with: URL(string: StringIncludeChineseToString(model?[indexPath.row].shareImageUrl ?? "")), completed: nil)
        cell.label.text = model?[indexPath.row].describe
        cell.label2.text = "\(model?[indexPath.row].listenNum ?? 0)"
        let frameNew = cell.label.text?.stringHeightWith(fontSize: 12, width: (SCREEN_WIDTH - 36) / 3)
        cell.label.frame = FRAME_CGFloat(2.5, (SCREEN_WIDTH - 36) / 3, (SCREEN_WIDTH - 36) / 3 - 5, frameNew ?? 38)
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let subModel = self.model.subProgram as! [OtherListenSubData]
        let id:Int = subModel[indexPath.row].id
        
        delegate?.HomeOthersListenCellClick(id: id)
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let headerView : OthersListenReusableView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: OthersListenReusableViewID, for: indexPath) as! OthersListenReusableView
        headerView.id = self.model.id
        headerView.leftLabel.text = self.model.title
        headerView.delegate = self
        return headerView
    }
    
    func OthersListenReusableViewClick(id: Int, name:String) {
        delegate?.HomeOthersListenHeaderCellClick(id: id, name:name)
    }
    
}

//  MARK: - ###### 私有方法 ######
extension HomeItemsView {
    
}
